package fuentes;
import java.util.Vector;

import javax.swing.JTextArea;


public class TablaSimbolos {
	private Vector<Simbolo> simbolos;
	private Vector<Simbolo> palabrasRes;
	private TablaSimbolos(){
		simbolos=new Vector<Simbolo>();
		palabrasRes=new Vector<Simbolo>();
		palabrasRes.addElement(new Simbolo("",new String("if")));
		palabrasRes.addElement(new Simbolo("",new String("then")));
		palabrasRes.addElement(new Simbolo("",new String("while")));
		palabrasRes.addElement(new Simbolo("",new String("else")));
		palabrasRes.addElement(new Simbolo("",new String("do")));
		palabrasRes.addElement(new Simbolo("",new String("print")));
		palabrasRes.addElement(new Simbolo("",new String("ulong")));
		palabrasRes.addElement(new Simbolo("",new String("float")));
	}
	private static TablaSimbolos tabla=null;
	
	public static TablaSimbolos getInstance(){
		if (tabla==null)
			tabla=new TablaSimbolos();
		return tabla;
	}
	
	public void AddSimbolo(Simbolo s){
		simbolos.addElement(s);
	}

	public boolean contains(Simbolo sim){
		return (simbolos.contains(sim));
	}
		
	public Simbolo getSimbolo(String aux){
		
		int i = getPosicion(aux);
		if (i!=-1)
			return simbolos.get(i);
		else
			return null;
		
	}
	private int getPosicion(String aux)
	{
		int i=0;
		boolean encontrado=false;
		while ((i<simbolos.size())&&(!encontrado))
		{
			if (simbolos.get(i).getValor().equals(aux))
			{
				encontrado=true;
			}else
			i++;
		}
		if (encontrado)
			return i;
		else
			return -1;
	}
	public Simbolo getSimbolo(Simbolo aux){
		return simbolos.elementAt(simbolos.indexOf(aux));
	}
	public boolean isPalRes(Simbolo aux){
		return (palabrasRes.contains(aux));
	}
	
	public Simbolo getPalRes(Simbolo aux){
		return palabrasRes.elementAt(palabrasRes.indexOf(aux));
	}

	public Vector<Simbolo> getSimbolos() {
		return simbolos;
	}


	public Vector<Simbolo> getPalabrasRes() {
		return palabrasRes;
	}

	public void mostrarTablaSim(JTextArea t){
		 for (int i = 0; i <simbolos.size(); i++) 
		 {
		 	 if (simbolos.elementAt(i).getValor()=="")
		 		 t.append("ID "+simbolos.elementAt(i).getValor());
		 	 else 
		 	 {
		 		 String aMostrar = "";
		 		 if (simbolos.elementAt(i).getNombre().equals("CTE") && simbolos.elementAt(i).getTipo().equals("float"))
		 			 aMostrar = simbolos.elementAt(i).getNombreVar();
		 		 else
		 			 aMostrar = simbolos.elementAt(i).getValor();
		 		 t.append(simbolos.elementAt(i).getNombre()+" Lexema: "+aMostrar+" Tipo: "+simbolos.elementAt(i).getTipo());
		 	 	 t.append("\n");
		 	 }
	 }
	}
	public void mostrarPalabrasRes(JTextArea t){
		for (int i = 0; i < palabrasRes.size(); i++) {
			t.append(palabrasRes.elementAt(i).getValor()+"\n");
		}
	}
	
	public void vaciar(){
		simbolos=new Vector<Simbolo>();
		
	}
	public void removerSimbolo(Simbolo aEliminar){
		
		int i = getPosicion(aEliminar.getValor());
		if (i!=-1){
			simbolos.remove(i);
		//	Simbolo s= this.getSimbolo(aEliminar.getValor());
		}
	}
	
}
